package com.file.controller;

import com.file.pojo.User;
import com.file.service.UserService;
import com.file.vo.PageResult;
import com.file.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    //用户展示
    @RequestMapping("/login")
    private SysResult login(@RequestBody User user){
        //执行后端登录操作,要求返回token密钥
        String token = userService.login(user);
        //判断token返回值是否有效
        if(StringUtils.hasLength(token)){
            return SysResult.success(token);
        }else{
            //登录失败
            return SysResult.fail();
        }
    }

    //分页展示

    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){
        pageResult = userService.selectUser(pageResult.getPageNum(),pageResult.getPageSize(), pageResult.getQuery());
        return SysResult.success(pageResult);
    }
    //用户状态修改
    //权限注解()
    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){
            userService.updateStatus(user);
            return SysResult.success();
    }
    //获取用户ID
    @GetMapping("/selectId/{id}")
    public SysResult getUserById(@PathVariable Integer id){
        User user = userService.getUserById(id);
        return SysResult.success(user);
    }
    //用户信息修改
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }
    //删除用户
    @DeleteMapping("/delete/{id}")
    public SysResult deleteUserById(@PathVariable Integer id){
        userService.deleteById(id);
        return SysResult.success();
    }
}
